package com.znsd.zl.common;

import com.znsd.zl.constant.CommonConstant;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.springframework.util.StringUtils;

import java.io.Serializable;

/**
 * @ClassName CommonResult
 * @Description TODO
 * @Author zhubin
 * @Date 2020/8/17 10:29
 * @Version v1.0
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class CommonResult<T> implements Serializable {

	/**
	 * 响应状态码
	 */
	private Integer code;

	/**
	 * 返回提示信息
	 */
	private String msg;

	/**
	 * 返回数据对象
	 */
	private T data;

	public static CommonResult success(){
		return new CommonResult(CommonConstant.SUCCESS_CODE, CommonConstant.SUCCESS,null);
	}

	/**
	 *  因为设置泛型，可以接收所有数据，默认返回Object
	 * @param data  所有返回参数
	 * @return 返回封装好的状态
	 */
	public static CommonResult success(Object data){
		return new CommonResult(CommonConstant.SUCCESS_CODE, CommonConstant.SUCCESS,data);
	}

	public static CommonResult success(String msg,Object data){
		return new CommonResult(CommonConstant.SUCCESS_CODE, StringUtils.isEmpty(msg) ? CommonConstant.SUCCESS : msg, data);
	}

	public static CommonResult error(String msg){
		return new CommonResult(CommonConstant.ERROR_CODE, StringUtils.isEmpty(msg) ? CommonConstant.ERROR : msg,null);
	}

	public static CommonResult error(){
		return new CommonResult(CommonConstant.ERROR_CODE, CommonConstant.ERROR,null);
	}


}
